﻿// *----------------------------------------------------------------
// Copyright (C) 2017 通通优品
// 版权所有。
// 
// 类名：ICache.cs
// 功能描述：TongTongMall
// 
// Create User：jym 2017/02/09
// 
// Edit User：UserName,EditTime
// Describe：Describe
// ----------------------------------------------------------------*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TongTongMall.Runtime.Caching
{
    public interface ICache : IDisposable
    {

        /// <summary>
        /// 缓存超时时间
        /// </summary>
        TimeSpan DefaultSlidingExpireTime { get; set; }

        /// <summary>
        /// 从缓存中获取对象
        /// </summary>
        /// <param name="key">Key</param>
        /// <param name="factory">如果不存在，工厂方法创建缓存项</param>
        /// <returns></returns>
        object Get(string key, Func<string, object> factory);

        /// <summary>
        /// 从缓存中获取对象
        /// </summary>
        /// <param name="key">Key</param>
        /// <param name="factory">如果不存在，工厂方法创建缓存项</param>
        /// <param name="expire">缓存过期时间</param>
        /// <returns></returns>
        Task<object> GetAsync(string key, Func<string, Task<object>> factory, TimeSpan? expire);

        /// <summary>
        /// 从缓存中获取对象不存在返回null
        /// </summary>
        /// <param name="key">Key</param>
        /// <returns></returns>
        object GetOrDefault(string key);

        /// <summary>
        /// 从缓存中获取对象不存在返回null
        /// </summary>
        /// <param name="key">Key</param>
        /// <returns></returns>
        T GetOrDefault<T>(string key);

        /// <summary>
        /// G从缓存中获取对象不存在返回null
        /// </summary>
        /// <param name="key">Key</param>
        /// <returns></returns>
        Task<object> GetOrDefaultAsync(string key);


        /// <summary>
        /// G从缓存中获取对象不存在返回null
        /// </summary>
        /// <param name="key">Key</param>
        /// <returns></returns>
        Task<T> GetOrDefaultAsync<T>(string key);

        /// <summary>
        /// 设置缓存
        /// </summary>
        /// <param name="key">Key</param>
        /// <param name="value">Value</param>
        /// <param name="slidingExpireTime">过期时间</param>
        void Set(string key, object value, TimeSpan? slidingExpireTime = null);

        /// <summary>
        /// 设置缓存
        /// </summary>
        /// <param name="key">Key</param>
        /// <param name="value">Value</param>
        /// <param name="slidingExpireTime">过期时间</param>
        Task SetAsync(string key, object value, TimeSpan? slidingExpireTime = null);

        /// <summary>
        /// 移除缓存
        /// </summary>
        /// <param name="key">Key</param>
        void Remove(string key);

        /// <summary>
        /// 移除缓存
        /// </summary>
        /// <param name="key">Key</param>
        Task RemoveAsync(string key);

        /// <summary>
        /// 请空全部缓存
        /// </summary>
        void Clear();

        /// <summary>
        /// 请空全部缓存
        /// </summary>
        Task ClearAsync();
    }
}
